From: Ivan Molodetskikh Date: Thu, 26 Aug 2021 07:39:46 +0000 (+0300) Subject: screenshot-ui: Store screenshots to files X-Git-Tag: archive/raspbian/50.5-1+rpi1~1^2~530^2^2~209 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=95df526996a3000078a84d4a802d2a5921f50bbd;p=gnome-shell.git screenshot-ui: Store screenshots to files Screenshots are always stored to (xdg-)Pictures/Screenshots as discussed in https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/1950#note_655669 Part-of: --- diff --git a/js/ui/screenshot.js b/js/ui/screenshot.js index a02ce8942..8b3c0656b 100644 --- a/js/ui/screenshot.js +++ b/js/ui/screenshot.js @@ -1530,6 +1530,62 @@ class ScreenshotUI extends St.Widget { this.close(); } + _storeScreenshot(bytes) { + // Store to the clipboard first in case storing to file fails. + const clipboard = St.Clipboard.get_default(); + clipboard.set_content(St.ClipboardType.CLIPBOARD, 'image/png', bytes); + + const dir = Gio.File.new_for_path(GLib.build_filenamev([ + GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES), + // Translators: name of the folder under ~/Pictures for screenshots. + _('Screenshots'), + ])); + + try { + dir.make_directory_with_parents(null); + } catch (e) { + if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) + throw e; + } + + const time = GLib.DateTime.new_now_local(); + const timestamp = time.format('%Y-%m-%d %H-%M-%S'); + // Translators: this is the name of the file that the screenshot is + // saved to. The placeholder is a timestamp, e.g. "2017-05-21 12-24-03". + const name = _('Screenshot from %s').format(timestamp); + + // If the target file already exists, try appending a suffix with an + // increasing number to it. + + /** + * Returns a filename suffix with an increasingly large index. + * + * @returns {Generator} suffix string + */ + function *suffixes() { + yield ''; + + for (let i = 1; ; i++) + yield '-%s'.format(i); + } + + let file; + for (const suffix of suffixes()) { + file = Gio.File.new_for_path(GLib.build_filenamev([ + dir.get_path(), '%s%s.png'.format(name, suffix), + ])); + + try { + const stream = file.create(Gio.FileCreateFlags.NONE, null); + stream.write_bytes(bytes, null); + break; + } catch (e) { + if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) + throw e; + } + } + } + _saveScreenshot() { global.display.get_sound_player().play_from_theme( 'screen-capture', _('Screenshot taken'), null); @@ -1559,13 +1615,7 @@ class ScreenshotUI extends St.Widget { stream ).then(() => { stream.close(null); - - const clipboard = St.Clipboard.get_default(); - clipboard.set_content( - St.ClipboardType.CLIPBOARD, - 'image/png', - stream.steal_as_bytes() - ); + this._storeScreenshot(stream.steal_as_bytes()); }).catch(err => { logError(err, 'Error capturing screenshot'); }); @@ -1598,13 +1648,7 @@ class ScreenshotUI extends St.Widget { stream ).then(() => { stream.close(null); - - const clipboard = St.Clipboard.get_default(); - clipboard.set_content( - St.ClipboardType.CLIPBOARD, - 'image/png', - stream.steal_as_bytes() - ); + this._storeScreenshot(stream.steal_as_bytes()); }).catch(err => { logError(err, 'Error capturing screenshot'); });